#include "Log.hpp"

namespace Log
{
  int logLevel = 0;

  std::ostream streamIntoTheVoid(0);

  void Init ()
  {
    ReadLogLevel();
  }

  void ReadLogLevel ()
  {
    Settings mySets = Settings::Load("resources/config/settings.ini");
    if (mySets.Contains("log_level"))
    { // read the level from the file
      logLevel = mySets.GetInt("log_level", 0);
    }
    else
    { // the log level was not in the file, put it there, and save
      mySets.SetInt("log_level", 0);
      mySets.Save();
      logLevel = 0;
    }
#ifdef _HARDCODED_LOG_ENABLED
    std::cout << "Log level = " << logLevel << ", ";
    if (logLevel >= 2)
      std::cout << "both error and info logging enabled\n";
    else if (logLevel >= 1)
      std::cout << "only error logging enabled\n";
    else
      std::cout << "all logging disabled\n";
#endif
  }

  std::ostream& GetInfoStream()
  {
#ifdef _HARDCODED_LOG_ENABLED
    if (logLevel >= 2)
      return std::cout;
    else
      return streamIntoTheVoid;
#else
      return streamIntoTheVoid;
#endif
  }

  std::ostream& GetErrorStream()
  {
#ifdef _HARDCODED_LOG_ENABLED
    if (logLevel >= 1)
      return std::cout;
    else
      return streamIntoTheVoid;
#else
      return streamIntoTheVoid;
#endif
  }
}